// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
var starter = angular.module('starter',['ionic','oc.lazyLoad', 'starter.router', 'starter.controllers', 'starter.services'])
        
    .run(function ($rootScope,$state,$window,settings,Page,$ionicPlatform,$location,ApiEndpoint,$ionicNavBarDelegate,$ionicBackdrop,sessionStore,localStore) {
    	$rootScope.user = {
    			openId : $window.openId,
    			access_token : $window.access_token
    	};
    	
    	if($rootScope.user.access_token && $rootScope.user.access_token != '') {
    		Page.ajaxPostQuiet('app/wx/getRegisterUser.do',{openId : $rootScope.user.openId}, function(resp) {
    			$rootScope.user = resp.data;
        	},function(resp){});
    	}
    	
    	
    	var url=$location.$$absUrl.split('#')[0];
        Page.ajaxPostQuiet('wx/jsapiSignature.do',{url: url}, function(resp) {
    		var signatureInfo = resp.data;
    		wx.config({
    	        debug: false,
    	        appId: signatureInfo.appId,
    	        timestamp: signatureInfo.timestamp,
    	        nonceStr: signatureInfo.nonceStr,
    	        signature: signatureInfo.signature,
    	        jsApiList: [
    	          'checkJsApi',
    	          'onMenuShareTimeline',
    	          'onMenuShareAppMessage',
    	          'onMenuShareQQ',
    	          'onMenuShareWeibo',
    	          'onMenuShareQZone',
    	          'hideMenuItems',
    	          'showMenuItems',
    	          'hideAllNonBaseMenuItem',
    	          'showAllNonBaseMenuItem',
    	          'translateVoice',
    	          'startRecord',
    	          'stopRecord',
    	          'onVoiceRecordEnd',
    	          'playVoice',
    	          'onVoicePlayEnd',
    	          'pauseVoice',
    	          'stopVoice',
    	          'uploadVoice',
    	          'downloadVoice',
    	          'chooseImage',
    	          'previewImage',
    	          'uploadImage',
    	          'downloadImage',
    	          'getNetworkType',
    	          'openLocation',
    	          'getLocation',
    	          'hideOptionMenu',
    	          'showOptionMenu',
    	          'closeWindow',
    	          'scanQRCode',
    	          'chooseWXPay',
    	          'openProductSpecificView',
    	          'addCard',
    	          'chooseCard',
    	          'openCard'
    	        ]
    	    });
    	});
    	
        wx.ready(function () {
        	wx.hideAllNonBaseMenuItem();
        });
    	
        $ionicPlatform.ready(function () {
            if (window.StatusBar) {
                StatusBar.styleDefault();
            }
        });

        $ionicBackdrop.release();
        $rootScope.$state = $state; // state to be accessed from view


        $rootScope.screenWidth = document.body.clientWidth;
        $rootScope.screenHeight = window.innerHeight;
        
        $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
            if (toState.name == 'login') {
            	$rootScope.user.access_token = null;
    		} else if (toState.auth && (!$rootScope.user || !$rootScope.user.access_token)) {
    			event.preventDefault();// 取消默认跳转行为
    			toState.params = toParams;
    			toParams.toState = toState;
    			$state.go("login", {'toState' : toState});
    		}       
        });
        
        $rootScope.$on('$stateChangeSuccess',function(event, toState, toParams, fromState, fromParams){
        	wx.ready(function () {
        		wx.hideAllNonBaseMenuItem();
           });
        });
    })
    
    .config(function ($stateProvider, $urlRouterProvider, $httpProvider, $ionicConfigProvider, $ocLazyLoadProvider) {
        $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
        $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
        $httpProvider.interceptors.push('HttpInterceptor');
        $httpProvider.interceptors.push('AuthInterceptor');

        $ionicConfigProvider.views.swipeBackEnabled(false);
        $ionicConfigProvider.views.transition('none');

        $ionicConfigProvider.platform.ios.tabs.style('standard');
        $ionicConfigProvider.platform.ios.tabs.position('bottom');
        $ionicConfigProvider.platform.android.tabs.style('standard');
        $ionicConfigProvider.platform.android.tabs.position('standard');

        $ionicConfigProvider.platform.ios.navBar.alignTitle('center');
        $ionicConfigProvider.platform.android.navBar.alignTitle('center');

        $ionicConfigProvider.platform.ios.backButton.previousTitleText('').icon('ion-ios-arrow-thin-left');
        $ionicConfigProvider.platform.android.backButton.previousTitleText('').icon('ion-android-arrow-back');

        $ionicConfigProvider.platform.ios.views.transition('ios');
        $ionicConfigProvider.platform.android.views.transition('android');

        $ocLazyLoadProvider.config({
            // global configs go here
    });

//	$stateProvider.files = {templates: {}, controller: {}};
//	$stateProvider.getTemplateByUrl = function(templateUrl) {
//	    if (!$stateProvider.files.templates[templateUrl]) {
//	    	var n = (new Date).getTime();
//	    	templateUrl = templateUrl.indexOf("?") >= 0 ? "&" === templateUrl.substring(0, templateUrl.length - 1) ? templateUrl + "_dc=" + n: templateUrl + "&_dc=" + n: templateUrl + "?_dc=" + n;
//	        $stateProvider.files.templates[templateUrl] = jQuery.ajax({url: templateUrl , async: false}).responseText;
//	    }
//	    return $stateProvider.files.templates[templateUrl];
//	};
});


